package com.xcity.game.peakroad.model.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.game.combat.CombatUtils;
import com.xcity.game.npc.NpcTeamTemplate;
import com.xcity.game.npc.NpcTemplate;
import com.xcity.game.peakroad.template.PeakRoadTemplate;

/**
 * 挑战层
 * @author yang.li
 *
 */
public class ChallengeFloorSub extends FloorSub {

	public List<Npc> npcs = Collections.emptyList();
	
	static final byte STATE_NOT_CHALLENGE = 0; // 未挑战
	static final byte STATE_CHALLENGING = 1; // 正处于该层
	static final byte STATE_CHALLENGED = 2; // 已挑战通过
	
	public static class Npc {
		public int id;
		public String name;
		public String icon;
		public int combatPower; // 该npc战队战斗力（所有npc战斗力总和）
		public Npc(NpcTeamTemplate npcTeam) {
			NpcTemplate template = npcTeam.getBoss();
			if (template == null) { // 没有boss则取第一个小怪
				NpcTemplate[] templates = npcTeam.getNpcs();
				for (int i = 0, len = templates != null ? templates.length : 0; i < len; i++) {
					template = templates[i];
					if (template != null) {
						break;
					}
				}
			}
			if (template == null) {
				throw new NullPointerException("the npc team config error, not found any npc template: " + npcTeam.getId());
			}
			this.id = template.getId();
			this.name = template.getName();
			this.icon = template.getIcon();
			this.combatPower = CombatUtils.calcCombatPower(npcTeam);
		}
	}
	
	public ChallengeFloorSub(Player player, PeakRoadTemplate refer) {
		int currentFloor = player.getPeakRoadInstance().getFloor();
		int floor = refer.getId();
		if (floor < currentFloor) {
			state = STATE_CHALLENGED;
		} else if (floor > currentFloor) {
			state = STATE_NOT_CHALLENGE;
		} else {
			state = STATE_CHALLENGING;
		}
		NpcTeamTemplate[] npcTeams = refer.getNpcTeams();
		if (npcTeams != null) {
			npcs = new ArrayList<Npc>(npcTeams.length);
			for (NpcTeamTemplate npcTeam : npcTeams) {
				npcs.add(new Npc(npcTeam));
			}
		}
	}
	
}
